const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter
const user_id = wx.getStorageSync('user_id')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_url:oss_domain + 'static/picture/app_icon/',
    page_no:0,
    ti_list:[],
    search_history_list:[],
    hot_search_list:[],
    search_value:'',
    no_more:false,
  },

  view_search_history(){
    this.setData({
      search_history_list_show:this.data.search_history_list
    })
  },

  // 清除历史搜索
  clear_trash(){
    wx.showModal({
      content: '确定要清除搜索历史记录吗？',
      showCancel: true,
      title: '提示',
      success: res => {
        if(res.confirm){
          wx.showLoading()
          wx.request({
            url: domainName + '/clear_search_rec_list',
            data: {
              user_id:user_id,
            },...parameter,
            success: res => {
              this.setData({
                search_history_list:res.data.search_history_list
              })
              wx.hideLoading()
            },
          })
        }
      },
    })
  },

  // 查看题详情
  view_ti_info(e){
    getApp().get_vibration()
    // console.log('查看题详情',e)
    let index = e.currentTarget.id
    let ti_id = this.data.ti_list[index].ti_id
    wx.navigateTo({
      url: '/pages/doTi/show_ti/show_ti?ti_id=' + ti_id
    })
  },

  // 输入搜索内容
  input_value(e){
    // console.log('输入搜索内容',e.detail.value)
    this.setData({
      search_value:e.detail.value
    })
  },

  // 清空搜索内容
  cancel(){
    // console.log('清空搜索内容')
    this.onLoad(this.options)
    
  },

  // 搜索推荐
  search_suggest(e){
    // console.log('搜索推荐',e)
    let search_value = e.currentTarget.id

    // 开始搜索
    this.search_ti(0,search_value)

    this.setData({
      search_value:search_value,
      ti_list:[],
      page_no:0,
    })
  },

  // 搜索
  search(e){
    let search_value = this.data.search_value.replace(/(^\s*)|(\s*$)/g,"")
    if(search_value){
      // 开始搜索
      this.setData({
        page_no:0
      })
      this.search_ti(this.data.page_no,search_value)
    }else{
      wx.showToast({
        title: '请先输入关键词',
        duration: 1000,
        icon: 'none',
        mask: false,
      })
    }

  },


  // 前往做题
  go_do_ti(){
    getApp().get_vibration()
    let ti_list = getApp().random_optList_of_tiku(this.data.ti_list) //随机乱序题库选项
    let url_data = 
    'tis=' + encodeURIComponent(JSON.stringify(ti_list || [])) + '&page_title=习题搜索'
    wx.navigateTo({
      url: '/pages/doTi/do_ti/do_ti?' + url_data,
    })

  },


  // 开始搜索内容
  search_ti(page_no,search_value){
    wx.showLoading({title: '大数据搜索中……'})
    wx.request({
      url: domainName + '/search_ti',
      data: {
        search_value:search_value,
        page_no:page_no,
        user_id:user_id,
      },...parameter,
      success: res => {
        if(res.data.search_rec_list_id){
          console.log('记录成功')
        }
        // console.log('开始搜索内容',res)
        let ti_list = res.data.ti_list
        let ti_all_count = res.data.ti_all_count
        if(this.data.page_no){
          this.setData({
            ti_list:this.data.ti_list.concat(ti_list),
            page_no:this.data.page_no + 1,
          })
        }else{
          this.setData({
            ti_list:ti_list,
            page_no:1,
          })
        }
        if(ti_list.length==0){
          this.setData({no_more:true})
        }else{
          this.setData({no_more:false})
        }
                
        this.setData({
          ti_all_count:ti_all_count,

          hot_search_list:res.data.hot_search_list,
          search_history_list:res.data.search_history_list,
          search_history_list_show:res.data.search_history_list.slice(0,10),
        })
      
        wx.hideLoading()
      },
    })
  },


  page_init(user_id){
    wx.showLoading()
    wx.request({
      url: domainName + '/search_ti_init',
      data: {
        user_id,
      },...parameter,
      success: res => {
        let search_history_list = res.data.search_history_list
        this.can_doti = res.data.can_doti
        this.setData({
          search_history_list:search_history_list,
          search_history_list_show:search_history_list && search_history_list.slice(0,10),
          hot_search_list:res.data.hot_search_list
        })
        wx.hideLoading()
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.page_init(user_id)
  },

  onReachBottom(){
    if(this.can_doti!=1){
      wx.showToast({
        title: '请联系教务',
        icon: 'error',
      })
      return
    }
    if(!this.data.no_more){
      this.search_ti(this.data.page_no,this.data.search_value)
    }
  }

})